import java.util.Random;

public class Scheduler 
{	
	private Random my_random_generator;
	private CPU my_cpu;
	private int my_list_index;
	
	private String my_scheduling_policy;
	
	public Scheduler(final String the_policy, final CPU the_cpu)
	{		
		my_cpu = the_cpu;
		my_scheduling_policy = the_policy;
		if (the_policy.equals("Lottery"))
		{
			my_random_generator = new Random();
			my_random_generator.setSeed(System.currentTimeMillis());
		}
		my_list_index = 0;
	}
	
	public int getListIndex()
	{
		return my_list_index;
	}
	
	public void setListIndex(final int the_index)
	{
		my_list_index = the_index;
	}
	
	public String getSchedulingPolicy()
	{
		return my_scheduling_policy;
	}
	
	public ProcessControlBlock getPCB(final int the_index)
	{
		return my_cpu.getPCBList().get(the_index);
	}
	
	public int getNextPCB()
	{	
		if (my_scheduling_policy.equals("Lottery"))
		{
			my_list_index = Math.abs(my_random_generator.nextInt() % my_cpu.getPCBList().size());
		}
		else if (my_scheduling_policy.equals("Priority"))
		{
			int max = getPCB(my_list_index).getProcessPriority();
			
			for (int i = 0; i < my_cpu.getPCBList().size(); i++)
			{
				if (my_cpu.getPCBList().get(i).getProcessPriority() > max)
				{
					max = my_cpu.getPCBList().get(i).getProcessPriority();
					my_list_index = i;
				}
			}
		}
		else
		{
			my_list_index = (my_list_index + 1) % my_cpu.getPCBList().size();
		}
		
		return my_list_index;
	}
}
